//
//  KuKu_RequestTool.m
//  kuku
//
//  Created by 清风徐来 on 2024/1/29.
//

#import "KuKu_RequestTool.h"
#import <ZBNetworking/ZBNetworking.h>

@implementation KuKu_RequestTool

+ (void)setupPublicParameters {

    #pragma mark -  公共配置
    [ZBRequestManager setupBaseConfig:^(ZBConfig * _Nullable config) {
        config.baseServer = @"";
        config.filtrationCacheKey = @[@"timeString"];
        config.requestSerializer = ZBJSONRequestSerializer;
        config.responseSerializer = ZBJSONResponseSerializer;
        config.timeoutInterval = 15;
        config.consoleLog = YES;
        config.responseContentTypes = @[@"application/pdf", @"video/mpeg4"];
        config.HTTPMethodsEncodingParametersInURI = [NSSet setWithObjects:@"GET", @"HEAD", nil];
    }];

    #pragma mark -  插件机制
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"github"] = @"https://github.com/Suzhibin/ZBNetworking";
    parameters[@"jianshu"] = @"https://www.jianshu.com/p/55cda3341d11";
    parameters[@"iap"] = @"0";
    NSMutableDictionary *headers = [NSMutableDictionary dictionary];
    [ZBRequestManager setRequestProcessHandler:^(ZBURLRequest * _Nullable request, id  _Nullable __autoreleasing * _Nullable setObject) {
        NSLog(@"插件响应 请求之前 可以进行参数加工,动态参数可在此添加");
        if ([request.parameters isKindOfClass:[NSDictionary class]]) {
            [request.parameters addEntriesFromDictionary:parameters];//此回调每次请求时调用一次，如果公共参数 可在此配置
        }
        //需要动态获取的参数 要在回调内取
        NSTimeInterval timeInterval = [[NSDate date] timeIntervalSince1970];
        NSString *timeString = [NSString stringWithFormat:@"%.2f",timeInterval];
        request.parameters[@"timeString"] = timeString;//时间戳
        request.headers = headers;
        request.headers[@"Token"] = @"Token";//如果请求头内的Token 是动态获取，比如登陆后获取的 ，在此设置Token
    }];

    // 预处理 响应
    [ZBRequestManager setResponseProcessHandler:^id(ZBURLRequest * _Nullable request, id  _Nullable responseObject, NSError * _Nullable __autoreleasing * _Nullable error) {
        NSLog(@"插件响应 成功回调 数据返回之前");
        return nil;
    }];

    // 预处理 错误
    [ZBRequestManager setErrorProcessHandler:^(ZBURLRequest * _Nullable request, NSError * _Nullable error) {
        if (error.code==NSURLErrorCancelled){
            NSLog(@"插件响应 请求取消❌------------------");
        }else if (error.code==NSURLErrorTimedOut){
            NSLog(@"插件响应 请求超时");
        }else{
            NSLog(@"插件响应 请求失败");
        }
    }];

    #pragma mark - 动态获取网络状态
    [ZBRequestManager setReachabilityStatusChangeBlock:^(ZBNetworkReachabilityStatus status) {
        switch (status) {
            case ZBNetworkReachabilityStatusUnknown:
                NSLog(@"未知网络");
                break;
            case ZBNetworkReachabilityStatusNotReachable:
                NSLog(@"断网");
                break;
            case ZBNetworkReachabilityStatusViaWWAN:
                NSLog(@"蜂窝数据");
                break;
            case ZBNetworkReachabilityStatusViaWiFi:
                NSLog(@"WiFi网络");
                break;
            default:
                break;
        }
    }];
}

//URL处理 不可见字符的问题 https://github.com/Suzhibin/ZBNetworking/issues/18
+(NSString *)urlProtect:(NSString *)url {
    if ([url containsString:@"\u200B"]) {
        return [url stringByReplacingOccurrencesOfString:@"\u200B" withString:@""];
    }
    return url;
}

@end
